package com.zjitc.controller;

import com.zjitc.common.Constant;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import com.zjitc.utils.MD5Utils;
import com.zjitc.utils.UUIDUtils;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * Created by LWW on 2018/4/19.
 */
@Controller
@RequestMapping("/user")
public class UserController {
  @Autowired
  private UserService userService;

  @RequestMapping("/login")
  public String login(@RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password,
      @RequestParam(name = "inputCode")String inputCode,
      HttpSession session){
    String msg= (String) session.getAttribute("msg");
    password=MD5Utils.md5(password);
    User users=userService.login(username,password);
    if(msg.equalsIgnoreCase(inputCode)){
      if (users==null){
        session.setAttribute("msg", "用户名或密码错误");
        return "/jsp/login.jsp";
      }else {
        if (Constant.USER_IS_ACTIVE != users.getState()) {
          session.setAttribute("msg", "用户还没有激活，请先去邮箱激活后登陆");
          return "/jsp/login.jsp";
        }
      }
        session.setAttribute("users",users);
        return "/jsp/index.jsp";
    }else {
      session.setAttribute("msg","验证码错误");
      return "/jsp/login.jsp";
    }
  }

  @RequestMapping("/logout")
  public String logout(HttpSession session){
    session.invalidate();
    return "/jsp/index.jsp";
  }

  @RequestMapping("/register")
  public String register(@RequestParam(name = "username")String username,
      @RequestParam(name = "password")String password,
      @RequestParam(name = "email")String email,
      @RequestParam(name = "name")String name,
      @RequestParam(name = "sex")String sex,
      @RequestParam(name = "birthday")String birthday,
      @RequestParam(name = "inputCode")String inputCode,
      HttpSession session,
      HttpServletRequest request)
      throws MessagingException, InvocationTargetException, IllegalAccessException, ParseException {
    String msg= (String) session.getAttribute("msg");
    User user=new User();
    if(msg.equalsIgnoreCase(inputCode)){
      user.setUid(UUIDUtils.getId());
      user.setCode(UUIDUtils.getCode());
      user.setUsername(username);
      user.setPassword(MD5Utils.md5(password));
      user.setEmail(email);
      user.setName(name);
      user.setSex(sex);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
      Date date=simpleDateFormat.parse(birthday);
      user.setBirthday(date);
      user.setState(0);
      boolean isRegister=userService.register(user);
      if(isRegister){
        session.setAttribute("msg","用户注册成功，请去邮箱激活");
        return "/jsp/msg.jsp";
      }
    }else{
      session.setAttribute("msg","验证码错误");
      return "/jsp/register.jsp";
    }
    return null;
  }


  @RequestMapping("/active")
  public String active(@RequestParam(name = "code") String code,
      HttpSession session){
    User active=userService.active(code);
    if(active==null){
      session.setAttribute("msg","激活失败请重新激活");
    }else{
      session.setAttribute("msg","激活成功将跳转到登陆页面");
    }
    return "/jsp/login.jsp";
  }
}
